05. Add map types
L4 A04 Adding Map Types
Add map types
Google Maps include several map types: normal, hybrid, satellite, terrain, and "none." In this task you add an app bar with an options menu that allows the user to change the map type. You move the map's starting location to your own home location. Then you add support for markers, which indicate single locations on a map and can include a label.
Add menu for map types
The type of map that your user wants depends on the kind of information they need. When using maps for navigation in your car, it's helpful to see street names clearly - in this case you would use the normal option. When you are hiking, you probably care more about how much you have to climb to get to the top of the mountain - in this case you would use the terrain option. In the Add app bar step, you add an app bar with an options menu that allows the user to change the map type.
- To create a new menu XML file, right-click on your res directory and select New > Android Resource File.
- In the dialog, name the file
map_options. Choose Menu for the resource type. Click OK.
- In the text tab, replace the code in the new file with the following code to create the map options. The "none" map type is omitted, because "none" results in the lack of any map at all.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/normal_map"
android:title="@string/normal_map"
app:showAsAction="never" />
<item
android:id="@+id/hybrid_map"
android:title="@string/hybrid_map"
app:showAsAction="never" />
<item
android:id="@+id/satellite_map"
android:title="@string/satellite_map"
app:showAsAction="never" />
<item
android:id="@+id/terrain_map"
android:title="@string/terrain_map"
app:showAsAction="never" />
</menu>
- In
strings.xml, add resources for thetitleattributes in order to resolve the errors.
<resources>
…
<string name="normal_map">Normal Map</string>
<string name="hybrid_map">Hybrid Map</string>
<string name="satellite_map">Satellite Map</string>
<string name="terrain_map">Terrain Map</string>
<string name="lat_long_snippet">Lat: %1$.5f, Long: %2$.5f</string>
<string name="dropped_pin">Dropped Pin</string>
<string name="poi">poi</string>
</resources>
- In
MapsActivity, override theonCreateOptionsMenu()method and inflate the menu from themap_optionsresource file:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.map_options, menu)
return true
}
- To change the map type, use the
setMapType()method on theGoogleMapobject, passing in one of themap-type constants.
InMapsActivity.kt, override theonOptionsItemSelected()method. Use the following code to change the map type when the user selects one of the menu options:
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
// Change the map type based on the user's selection.
R.id.normal_map -> {
map.mapType = GoogleMap.MAP_TYPE_NORMAL
true
}
R.id.hybrid_map -> {
map.mapType = GoogleMap.MAP_TYPE_HYBRID
true
}
R.id.satellite_map -> {
map.mapType = GoogleMap.MAP_TYPE_SATELLITE
true
}
R.id.terrain_map -> {
map.mapType = GoogleMap.MAP_TYPE_TERRAIN
true
}
else -> super.onOptionsItemSelected(item)
}
- Run the app. Use the menu in the app bar to change the map type. Notice how the map's appearance changes between the different modes.